package com.block.account.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.account.model.dto.EntryAccountDTO;
import com.block.account.service.IAccountActionHandleService;
import com.block.account.service.IAccountItemProductHisService;
import com.block.account.service.IAccountItemProductService;
import com.block.common.service.LogFactory;
import com.block.common.util.BigDecimalUtil;
import com.block.db.entity.AccountItemProduct;
import com.block.db.mapper.AccountItemProductHisMapper;
import com.block.db.mapper.AccountItemProductMapper;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 * 产品账户信息表 服务实现类
 * </p>
 *
 * @author 
 * @since 2023-02-02
 */
@Service
public class AccountItemProductServiceImpl extends ServiceImpl<AccountItemProductMapper, AccountItemProduct> implements IAccountItemProductService, IAccountActionHandleService {

    private static Logger log = LogFactory.getCommLog();

    @Resource
    AccountItemProductHisMapper accountItemProductHisMapper;
    @Autowired
    IAccountItemProductHisService accountItemProductHisService;


    /**
     * 获取产品账户信息
     * @param productId
     * @param productName
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public AccountItemProduct getAccountInfoByProductId(String productId, String productName) {

        AccountItemProduct accountItemProduct = this.baseMapper.getEntityByProductIdLock(productId);
        if (ObjectUtil.isEmpty(accountItemProduct)){
            accountItemProduct = this.saveAccountInfo(productId, productName);
        }
        return accountItemProduct;
    }

    public AccountItemProduct saveAccountInfo(String productId, String productName){

        AccountItemProduct newEntity = new AccountItemProduct();
        newEntity.setProductId(productId);
        newEntity.setProductName(productName);

        boolean save = this.save(newEntity);
        return this.getById(newEntity.getId());

    }

    /**
     * 查询产品某日的预付金额（已入账 的 预付金额）
     * @param dateInfo 如果dateInfo
     * @param productId
     * @return
     */
    @Override
    public BigDecimal queryPayAmount(String dateInfo, String productId){
        return accountItemProductHisMapper.queryPayAmountByDateInfoAndProductId(dateInfo, productId);
    }


    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public boolean entryAccountInfo(EntryAccountDTO entryAccountDTO) {
        AccountItemProduct accountInfo =
                this.getById(entryAccountDTO.getItemAccountId());

        BigDecimal avaliableAmount = BigDecimalUtil.add(entryAccountDTO.getChangeAmount(), accountInfo.getAvaliableAmount());
        BigDecimal totalAmount = BigDecimalUtil.add(entryAccountDTO.getChangeAmount(), accountInfo.getTotalAmount());

        AccountItemProduct updateEntity = new AccountItemProduct();
        updateEntity.setId(accountInfo.getId());
        updateEntity.setAvaliableAmount(avaliableAmount);
        updateEntity.setTotalAmount(totalAmount);
        boolean updateRes = this.updateById(updateEntity);
        log.info("【渠道账户入账】更新产品账户完成【updateRes】{}【channelCode】{}【channelName】{}【actionType】{}【changeAmount】{}【recordId】{}",
                updateRes, entryAccountDTO.getItemAccountKey(), entryAccountDTO.getItemAccountName(),
                entryAccountDTO.getActionType(), entryAccountDTO.getChangeAmount(), entryAccountDTO.getRecordId());
        if (!updateRes){
            return false;
        }

        entryAccountDTO.setOriginAmount(accountInfo.getAvaliableAmount());
        entryAccountDTO.setCurrentAmount(avaliableAmount);
        boolean hisRes = accountItemProductHisService.saveProductHisInfo(entryAccountDTO);
        log.info("【渠道账户入账】新增产品账户历史【hisRes】{}【channelCode】{}【channelName】{}【actionType】{}【changeAmount】{}【recordId】{}",
                hisRes, entryAccountDTO.getItemAccountKey(), entryAccountDTO.getItemAccountName(),
                entryAccountDTO.getActionType(), entryAccountDTO.getChangeAmount(), entryAccountDTO.getRecordId());
        if (!hisRes){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return hisRes;
    }
}
